package de.pyranja.storage.support;

public final class HexUtil {
	// static helper class
	private HexUtil() { }
	
	// all hex characters
	static final char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	
	// taken from stackoverflow 
	// http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java
	public static String toHex(final byte[] bytes) {
		final char[] result = new char[bytes.length << 1];	// * 2
		for (int i = 0; i < bytes.length; i++) {
			final int v = bytes[i] & 0xFF;		// mask to two hex chars == 8 bit == 1 byte
			final int j = i << 1;				// * 2 == result index
			result[j] = HEX_CHARS[v >>> 4];		// 4 MSBs in current byte to hex char -> shifts upper 4 bit to right and fills with zeros
			result[j+1] = HEX_CHARS[v & 0x0F];	// 4 LSBs in current byte to hex char -> uses bit mask 
		}
		return new String(result);
	}
}
